package com.rahms.server.controller;

import com.rahms.common.result.R;
import com.rahms.pojo.entity.CommunityAnnouncement;
import com.rahms.server.service.CommunityAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社区公告控制器
 */
@RestController
@RequestMapping("/announcement")
public class CommunityAnnouncementController {
    
    @Autowired
    private CommunityAnnouncementService communityAnnouncementService;
    
    /**
     * 获取最新的公告列表
     */
    @GetMapping("/latest")
    public R<List<CommunityAnnouncement>> getLatestAnnouncements(@RequestParam(required = false, defaultValue = "10") Integer limit) {
        List<CommunityAnnouncement> list = communityAnnouncementService.getLatestAnnouncements(limit);
        return R.success(list);
    }
    
    /**
     * 根据分类获取公告列表
     */
    @GetMapping("/by-category")
    public R<List<CommunityAnnouncement>> getAnnouncementsByCategory(@RequestParam String category,
                                                                  @RequestParam(required = false, defaultValue = "10") Integer limit) {
        List<CommunityAnnouncement> list = communityAnnouncementService.getAnnouncementsByCategory(category, limit);
        return R.success(list);
    }
    
    /**
     * 获取公告详情并增加阅读量
     */
    @GetMapping("/detail")
    public R<CommunityAnnouncement> getAnnouncementDetail(@RequestParam Long announcementId) {
        // 增加阅读量
        communityAnnouncementService.increaseViewCount(announcementId);
        // 获取公告详情
        CommunityAnnouncement announcement = communityAnnouncementService.getById(announcementId);
        return announcement != null ? R.success(announcement) : R.error("公告不存在");
    }
    
    /**
     * 增加公告阅读量
     */
    @PostMapping("/increase-view")
    public R<Boolean> increaseViewCount(@RequestParam Long announcementId) {
        boolean result = communityAnnouncementService.increaseViewCount(announcementId);
        return result ? R.success(true) : R.error("操作失败");
    }
}